from manimlib import *


class DraggableSquare(MotionMobject):

    def __init__(self, index: int, side_length=2, **kwargs):
        mobject = VGroup(
            Square(side_length, **kwargs),
            Integer(index)
        )
        super().__init__(mobject, **kwargs)
        self.mobject.add_mouse_press_listner(self.mob_on_mouse_press)

    def mob_on_mouse_press(self, mob: Mobject, event_data):
        self.data["offset"] = -event_data["point"] + mob.get_center()
        return False

    def mob_on_mouse_drag(self, mob: Mobject, event_data):
        mob.move_to(event_data["point"] + self.data["offset"])
        return False
